﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Model.Admin;
using Xproject.Framework.Utility.Extensions;
using Xproject.Framework.Core.Repositories;
using Xproject.Framework.Utility.ExceptionHandling;
using Xproject.Framework.Core;
using System.Linq.Expressions;
using Model.Admin.DTO;

namespace Repository.WXProjectAdmin
{
    public static class AdminMenuInfoRep
    {
        #region Get
        public static AdminMenu Get(this IRepositoryAsync<AdminMenu> repository, string id)
        {
            Expression<Func<AdminMenu, bool>> express = i => 1 == 1;
            if (!string.IsNullOrEmpty(id))
            {
                express = express.AndAlso(i => i.ID == id);
            }
            return repository.Query().Where(express).Select().OrderByDescending(i => i.CreateTime).FirstOrDefault();
        }
        public static AdminMenu GetByMenuId(this IRepositoryAsync<AdminMenu> repository, int  menuId)
        {
            Expression<Func<AdminMenu, bool>> express = i => 1 == 1;
           
                express = express.AndAlso(i => i.MenuId == menuId);
          
            return repository.Query().Where(express).Select().OrderByDescending(i => i.CreateTime).FirstOrDefault();
        }
        public static AdminRole Get(this IRepositoryAsync<AdminRole> repository, string id)
        {
            Expression<Func<AdminRole, bool>> express = i => 1 == 1;
            if (!string.IsNullOrEmpty(id))
            {
                express = express.AndAlso(i => i.ID == id);
            }
            return repository.Query().Where(express).Select().OrderByDescending(i => i.CreateTime).FirstOrDefault();
        }

        public static AdminRoleMenu Get(this IRepositoryAsync<AdminRoleMenu> repository, string id)
        {
            Expression<Func<AdminRoleMenu, bool>> express = i => 1 == 1;
            if (!string.IsNullOrEmpty(id))
            {
                express = express.AndAlso(i => i.ID == id);
            }
            return repository.Query().Where(express).Select().FirstOrDefault();
        }

        public static AdminUserRole Get(this IRepositoryAsync<AdminUserRole> repository, string id)
        {
            Expression<Func<AdminUserRole, bool>> express = i => 1 == 1;
            if (!string.IsNullOrEmpty(id))
            {
                express = express.AndAlso(i => i.ID == id);
            }
            return repository.Query().Where(express).Select().FirstOrDefault();
        }
    

        #endregion

       

        public static List<AdminMenu> GetAllMenuList(this IRepositoryAsync<AdminMenu> repository, string site,string menuName,string menuDesc) {
            Expression<Func<AdminMenu, bool>> express = i => 1 == 1;
            express = express.AndAlso(i=> i.Enable==1 && i.Site==site);
            if (!string.IsNullOrEmpty(menuName))
            {
                express = express.AndAlso(i =>i.MenuName.Contains(menuName));
            }
            if (!string.IsNullOrEmpty(menuDesc))
            {
                express = express.AndAlso(i => i.MenuDesc.Contains(menuDesc));
            }
            return repository.Query().Where(express).Select().OrderByDescending(i => i.MenuOrder).ToList();
        }
    }
}
